package com.sjm.suchbear.minibookmall;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.Utils.SP;
import com.sjm.suchbear.minibookmall.Utils.T;
import com.sjm.suchbear.minibookmall.action.UserManage;
import com.sjm.suchbear.minibookmall.manage.ManageActivity;
import com.sjm.suchbear.minibookmall.model.UserModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends Activity {

    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_register)
    TextView tvRegister;

    private SQLiteDatabase dbread;

    private static int GO_REJISTER = 0x111;
    private static int LOGIN_RESULT = 0x222;

    private String username;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        BookDB db = new BookDB(getBaseContext());
        dbread = db.getReadableDatabase();
        llHeaderBack.setVisibility(View.GONE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!SP.getUid(getBaseContext()).equals("")) {
            //自动登录
            login(SP.getLoginUserName(getBaseContext()), SP.getPassword(getBaseContext()));
        }
    }

    @OnClick({R.id.tv_login, R.id.tv_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                login();
                break;
            case R.id.tv_register:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, GO_REJISTER);
                break;
        }
    }

    private void login() {
        username = String.valueOf(etUsername.getText());
        password = String.valueOf(etPassword.getText());
        if (username.trim().equals("")) {
            T.showShort(getBaseContext(), "用户名不得为空");
            etUsername.requestFocus();
            return;
        }
        if (password.trim().equals("")) {
            T.showShort(getBaseContext(), "密码不得为空");
            etPassword.requestFocus();
            return;
        }
        UserModel userModel = UserManage.findUserByName(dbread, username, password);
        if (userModel != null) {
            T.showShort(getBaseContext(), "登录成功");
            SP.setLoginUser(getBaseContext(), username, userModel.getUserID(), password);
            if (userModel.getAuth() == 0) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            } else if (userModel.getAuth() == 1) {
                Intent intent = new Intent(LoginActivity.this, ManageActivity.class);
                startActivity(intent);
                finish();
            }
        } else {
            T.showShort(getBaseContext(), "登录失败");
        }
    }

    private void login(String username, String password) {
        UserModel userModel = UserManage.findUserByName(dbread, username, password);
        if (userModel != null) {
            T.showShort(getBaseContext(), "登录成功");
            if (userModel.getAuth() == 0) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            } else if (userModel.getAuth() == 1) {
                Intent intent = new Intent(LoginActivity.this, ManageActivity.class);
                startActivity(intent);
                finish();
            }
        } else {
            T.showShort(getBaseContext(), "登录失败");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GO_REJISTER && resultCode == LOGIN_RESULT) {
            String backUsername = data.getStringExtra("loginUsername");
            etUsername.setText(backUsername);
            etPassword.requestFocus();
        }
    }
}
